package com.zc58s.springbootmqtt.mqtt.distributor.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zc58s.springbootmqtt.mqtt.MqttBundle;
import com.zc58s.springbootmqtt.mqtt.Webtalk;
import com.zc58s.springbootmqtt.mqtt.distributor.service.IDistributorService;
import com.zc58s.springbootmqtt.mqtt.vo.SubcribeRealtimeMessageRes;
import com.zc58s.springbootmqtt.mqtt.vo.SubcribeRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Webtalk向第三方推送实时数据处理接口
 *
 * @author : fujc-dev@qq.com
 * @created : 2022/9/22 14:27
 */

@Component("WSN/CSN/pub/data/req")
public class WSNCSNpubdatareq implements IDistributorService {

    @Autowired
    private MqttBundle mqttBundle;
    @Override
    public void distributor(SubcribeRes res) {
        SubcribeRealtimeMessageRes req = new SubcribeRealtimeMessageRes();
        req.setSn(res.getSn());
        req.setCloudsn(res.getCloudsn());
        req.setWbtksn(res.getWbtksn());
        req.setCode(0);
        ObjectMapper mapper = new ObjectMapper();
        String str = null;
        try {
            str = mapper.writeValueAsString(req);
            mqttBundle.publish(Webtalk.CSN_WSN_pub_data_res, str);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
